//
//  ZCScanChooseVC.m
//  zctx
//
//  Created by apple on 16/3/17.
//  Copyright © 2016年 sanliang. All rights reserved.
//

#import "ZCScanChooseVC.h"
#define TABLEVIEWMARGE 19
#import "ZCScanFootView.h"
#import <QuartzCore/QuartzCore.h>

@interface ZCScanChooseVC ()<UITableViewDataSource,UITableViewDelegate,UIWebViewDelegate>

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)UIImageView *headImgView;

@property (nonatomic,strong)UILabel *titleLable;

@property (nonatomic,strong)UIWebView *webView;

@property (nonatomic,strong)UIView *footView;

@property (nonatomic,strong)NSString *toBody;

@end

@implementation ZCScanChooseVC

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
    
    self.toBody = [NSString stringWithFormat:@"%@",[self.dataChooseDict objectForKey:@"content"]];
    
    
    if ([NSString isNull:self.toBody] ==NO) {
        [self.view addSubview:self.tableView];
        [self WebViewBody:self.toBody];
        
    }else{
        ZCScanFootView *footView = [[ZCScanFootView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
        [self.view addSubview:footView];
    }
}

-(void)WebViewBody:(NSString *)str{
    
    
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]};
        
        NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[NSData dataWithData:[str dataUsingEncoding:NSUTF8StringEncoding]] options:options documentAttributes:nil error:nil];
        
        NSString *resplacString = [NSString stringWithFormat:@"%@%@",@"src=\"",[NetworkManager strURLBasic]];
        //            NSLog(@"resplacString = %@",resplacString);
        NSString *strHtml = [attrStr.string stringByReplacingCharactersInRangeString:attrStr.string andString:@"src=\"" andorString:resplacString];
        
        NSString *string = [NSString showInWebView:strHtml];
    
    NSLog(@"self.toBody111 = %@",string);
        
//        dispatch_async(dispatch_get_main_queue(), ^{
            [self.webView loadHTMLString:string baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];
//        });
//    });
    
    
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *str = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell ==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH - 2 *TABLEVIEWMARGE,44)];
    headView.backgroundColor = [UIColor whiteColor];
    headView.layer.cornerRadius = 5;

//    [headView addSubview:self.titleLable];
//    [self.titleLable makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.equalTo(headView).with.offset(0);
//        make.top.equalTo(headView).with.offset(8);
//        make.height.mas_equalTo(25);
//        make.width.lessThanOrEqualTo(@(SCREEN_WIDTH - 9 *TABLEVIEWMARGE));
//    }];
//    self.titleLable.text = [NSString stringWithFormat:@"%@",[self.dataChooseDict objectForKey:@"title"]];
    
    self.titleLable.text = [NSString stringWithFormat:@"%@",[self.dataChooseDict objectForKey:@"title"]];
    
    [headView addSubview:self.titleLable];
    self.titleLable.frame = CGRectMake(0, 0, 200, 25);
    [self.titleLable sizeToFit];
    self.titleLable.center = CGPointMake(self.tableView.frame.size.width/2,22) ;
    CGFloat titleW = self.titleLable.frame.size.width;
    
    if (IS_IPHONE_4_OR_LESS ||IS_IPHONE_5) {
        if (titleW>140) {
            CGRect frame = self.titleLable.frame;
            frame.size.width = 140;
            self.titleLable.frame = frame;
            self.titleLable.center = CGPointMake(self.tableView.frame.size.width/2,22) ;
        }
    }else{
        if (titleW>200) {
            CGRect frame = self.titleLable.frame;
            frame.size.width = 200;
            self.titleLable.frame = frame;
            self.titleLable.center = CGPointMake(self.tableView.frame.size.width/2,22) ;
        }
    }
    
    [headView addSubview:self.headImgView];
    [self.headImgView makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(headView).with.offset(0);
        make.top.equalTo(headView).with.offset(10);
        make.height.mas_equalTo(25);
        make.left.equalTo(self.titleLable.left).with.offset(-30);
    }];
    [headView bringSubviewToFront:self.titleLable];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(TABLEVIEWMARGE,43,self.tableView.frame.size.width - 2 * TABLEVIEWMARGE, 1)];
    [headView addSubview:view];
    [UIView drawDashLine:view lineLength:3 lineSpacing:3 lineColor:[UIColor colorWithHexString:@"#B3EEEE"]];

    return headView;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return self.webView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    
    return self.webView.frame.size.height;
}

#pragma mark webView 加载完成
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    
    if (self.webView.isLoading) {
        return;
    }
    NSLog(@"webview1 = %@",webView);
    
//    int totalHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollHeight"] intValue];
//    
//    CGRect frame = self.webView.frame;
//    frame.size.height = totalHeight;
//    self.webView.frame = frame;
    
    
    CGRect frame = self.webView.frame;
    NSLog(@"height 11=%f = %f",self.webView.frame.origin.y,self.webView.frame.size.height);
    
    CGSize fittingSize = [self.webView sizeThatFits:CGSizeMake(SCREEN_WIDTH-38, MAXFLOAT)];
    frame.size.height = fittingSize.height;
    frame.size.width = SCREEN_WIDTH-38;
    self.webView.frame = frame;
    
    NSLog(@"height 1122=%f = %f",self.webView.frame.size.width,self.webView.frame.size.height);
    
    [self.tableView reloadData];

}

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(TABLEVIEWMARGE, 23, SCREEN_WIDTH - 2 *TABLEVIEWMARGE,SCREEN_HEIGHT-130 -64-23-20) style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.clipsToBounds = NO;

        _tableView.layer.shadowColor = [UIColor colorWithHexString:@"#748080"].CGColor;
        _tableView.layer.cornerRadius = 5;
        _tableView.layer.shadowRadius = 1;
        _tableView.layer.shadowOpacity = 0.5; //设置阴影的不透明度
        _tableView.layer.shadowOffset = CGSizeMake(0, 1);
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.bounces = NO;
    }
    return _tableView;
}

-(UIImageView *)headImgView{
    if (!_headImgView) {
        _headImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
        UIImage *image = [UIImage imageNamed:@"scan_green"];
        image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 15)];
        _headImgView.image = image;
    }
    return _headImgView;
}

-(UILabel *)titleLable{
    if (!_titleLable) {
        _titleLable = [[UILabel alloc]initWithFrame:CGRectZero];
        _titleLable.text = @"谷物类膨化食品选购指南";
        _titleLable.font = [UIFont systemFontOfSize:16];
        _titleLable.textColor = [UIColor whiteColor];
        _titleLable.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLable;
}

//-(UIWebView *)webView{
//    if (!_webView) {
//        _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0,0,100,500)];
//        _webView.delegate = self;
//        _webView.scrollView.bounces = NO;
//        _webView.scrollView.scrollEnabled = NO;
//    }
//    return _webView;
//}


-(UIWebView *)webView{
    if (!_webView) {
        _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH - 2 *TABLEVIEWMARGE,SCREEN_HEIGHT)];
        _webView.delegate = self;
        _webView.scrollView.bounces = NO;
        _webView.tag = 2;
        _webView.scrollView.scrollEnabled = NO;
    }
    return _webView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
